package com.bw.educate.controller;



import com.bw.educate.common.ResponseResult;
import com.bw.educate.entity.EduUser;
import com.bw.educate.service.IEduUserService;
import com.bw.educate.util.TestImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;




/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/student")

public class EduUserController {

    @Autowired
    private IEduUserService userService;

    @Autowired
    private TestImport testImport;

    /**
     * @Description:查询学员列表
     * @Author: 邢晋宁
     * @Date: 2020/8/14 8:44
     * @param user:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("list/{currentPage}/{pageSize}")
    public ResponseResult list(@PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize, @RequestBody EduUser user){
        return userService.getList(currentPage,pageSize,user);
    }

    /**
     * @Description:学员添加
     * @Author: 邢晋宁
     * @Date: 2020/8/14 8:44
     * @param user:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("addStudent")
    public ResponseResult addStudent(@RequestBody EduUser user){
        return userService.addStudent(user);
    }

    /**
     * @Description:冻结学员
     * @Author: 邢晋宁
     * @Date: 2020/8/14 8:56
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("freezeStudnet/{id}")
    public ResponseResult freezeStudnet(@PathVariable("id") Long id){

        return userService.freezeStudnet(id);
    }

    /**
     * @Description:删除学员
     * @Author: 邢晋宁
     * @Date: 2020/8/14 8:56
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("delStudnet/{id}")
    public ResponseResult delStudnet(@PathVariable("id") Long id){

        return userService.delStudnet(id);
    }

    /**
     * @Description:根据id查询学员
     * @Author: 邢晋宁
     * @Date: 2020/8/14 9:01
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("findStudent/{id}")
    public ResponseResult findStudent(@PathVariable("id") Long id){
        return userService.findStudent(id);
    }

    /**
     * @Description:修改学员
     * @Author: 邢晋宁
     * @Date: 2020/8/14 9:04
     * @param user:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("updateStudent")
    public ResponseResult updateStudent(@RequestBody EduUser user){
        return userService.userService(user);
    }


    /**
     * @Description:学员头像上传
     * @Author: 邢晋宁
     * @Date: 2020/8/14 9:04
     * @param file:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("upload")
    public ResponseResult upload(MultipartFile file){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            Map map = testImport.upload(file);
            result.setResult(map);
            result.setMessage("上传成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("学员头像上传失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:学员头像回显
     * @Author: 邢晋宁
     * @Date: 2020/8/14 9:04
     * @param path,response:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("download/{path}")
    public void download(@PathVariable("path") String path, HttpServletResponse response){

        try {
            testImport.downLoad(path,response);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * @Description:学员的冻结或者启用
     * @Author: 邢晋宁
     * @Date: 2020/8/14 9:04
     * @param id,isAvalible:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("updateisAvalible/{id}/{isAvalible}")
    public ResponseResult download(@PathVariable("id") long id, @PathVariable("isAvalible") long isAvalible){

        return userService.updateisAvalible(id,isAvalible);

    }

    /**
     * @Description:C端学院登录
     * @Author: 王文佳
     * @Date: 2020/8/19 8:13
     * @param user:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("login")
    public ResponseResult login(@RequestBody EduUser user, HttpServletResponse response){
        return userService.login(user,response);
    }

    /**
     * @Description:C端学员注册
     * @Author: 王文佳
     * @Date: 2020/8/20 8:37
     * @param user:
     * @param :
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("registerLogin")
    public ResponseResult registerLogin(@RequestBody EduUser user){
        return userService.registerLogin(user);
    }


}
